package tech.aistar.controller.booking;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.*;
import tech.aistar.service.booking.IBookingService;
import tech.aistar.service.newService.INewsService;
import tech.aistar.service.order.IServiceOrderService;
import tech.aistar.service.services.IServicesService;
import tech.aistar.util.MakeOrderNum;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/10/12
 * @Version: 1.0
 */
@Controller
@RequestMapping("/services")
public class BookController {
    @Autowired
    private IBookingService bookingService;

    @Autowired
    private IServiceOrderService serviceOrderService;

    @Autowired
    private IServicesService servicesService;

    @Autowired
    private INewsService newsService;


    //获取服务总价
    @ResponseBody
    @RequestMapping("/getPrice")
    public Result getPrice(Model model, String value, Double price, String chargetype, HttpSession session) {
        String[] times = value.split(" ~ ");
        String beginTime = times[0];
        String endTime = times[1];
        Double sum = 0.0;
        if ("1".equals(chargetype)) {
            Integer diffmonth = daysBetween1(beginTime, endTime);
            sum = diffmonth * price;
            if(diffmonth == 0){
                sum = price;
            }
        }
        if ("2".equals(chargetype)) {
            Integer diffday = daysBetween2(beginTime, endTime);
            sum = diffday * price;
            if(diffday == 0){
                sum = price;
            }
        }
        if ("3".equals(chargetype)) {
            Integer diffhour = daysBetween3(beginTime, endTime);
            sum = diffhour * price;
            if(diffhour == 0){
                sum = price;
            }
        }
        return new Result(CodeStatus.OK, true, "success", sum);
    }

    //计算两个日期的月数差
    public static int daysBetween1(String startTime, String endTime) {
        String[] startTimes = startTime.split("-");
        String[] endTimes = endTime.split("-");
        Integer startTime1 = Integer.valueOf(startTimes[0]);
        Integer startTime2 = Integer.valueOf(startTimes[1]);
        Integer endTime1 = Integer.valueOf(endTimes[0]);
        Integer endTime2 = Integer.valueOf(endTimes[1]);

        Integer diff = (endTime1 - startTime1) * 12 + endTime2 - startTime2;

        return diff;
    }

    //计算两个日期的天数差
    public static int daysBetween2(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        long time1 = 0;
        long time2 = 0;

        try {
            cal.setTime(sdf.parse(startTime));
            time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(endTime));
            time2 = cal.getTimeInMillis();
        } catch (Exception e) {
            e.printStackTrace();
        }
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    //计算两个日期的小时差
    public static int daysBetween3(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        Calendar cal = Calendar.getInstance();
        long time1 = 0;
        long time2 = 0;

        try {
            cal.setTime(sdf.parse(startTime));
            time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(endTime));
            time2 = cal.getTimeInMillis();
        } catch (Exception e) {
            e.printStackTrace();
        }
        long between_days = (time2 - time1) / (1000 * 3600);

        return Integer.parseInt(String.valueOf(between_days));
    }

    //预约服务
    @ResponseBody
    @RequestMapping("/book")
    public Result book(@RequestBody Booking booking, HttpSession session) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
//        String bookTime = sdf.format(booking.getBookTime());
//        System.out.println("bookTime = " + bookTime);
//        try {
//            booking.setBookTime(sdf.parse(bookTime));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        生成订单
        ServiceOrder serviceOrder = new ServiceOrder();
        String serviceOrderNum = MakeOrderNum.makeOrderNum();
        serviceOrder.setOrderNumber(serviceOrderNum);
        serviceOrder.setServicesId(booking.getServicesId());
        serviceOrder.setUserinfoId(booking.getUserinfoId());

        System.out.println("booking.getBookTime() = " + booking.getBookTime());//打印预约时间
        serviceOrder.setCommitTime(booking.getBookTime());
        serviceOrder.setTotal(booking.getPrice());
        serviceOrder.setStatus(1);
        serviceOrderService.insertServiceOrder(serviceOrder);

        Services services = servicesService.findById(booking.getServicesId());
        String serviceName = services.getTitle();
        session.setAttribute("serviceName",serviceName);
        session.setAttribute("serviceOrderNum",serviceOrderNum);
        session.setAttribute("servicePrice",booking.getPrice());
        session.setAttribute("",booking.getServicesId());

        //生成消息
        News news = new News();
        Integer UIID = (Integer) session.getAttribute("userinfoId");
        news.setUserinfoId(UIID);
        news.setNewsTime(booking.getBookTime());
        news.setNewsContent(booking.getIntro());
        news.setStatus(0);
        news.setSerid(services.getUserinfoId());
        //news.setUid(booking.getUserinfoId());

        newsService.newsAdd(news);

        System.out.println("booking = " + booking);
        bookingService.add(booking);

        return new Result(CodeStatus.OK, true, "成功!", booking);
    }

//    @ResponseBody
//    @RequestMapping("/pay")
//    public String pay() {
//
//        return "pay/payPage";
//    }
    //生成订单
    @GetMapping("/pay")
    public String pay(){

        return "pay/payPage";
    }

}